package com.wjm.serversocket.test1;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.NumberFormat;

/**
 * Socket java程序间 上传文件，单线程
 */
public class UploadPicClient {
//    public static String fileNameStart = "a_100mb";
//    public static String fileMp4 = ".mp4";
//    public static String fileName = fileNameStart + fileMp4;
//    public static String fileStr = "i:" + File.separator + fileNameStart + fileMp4;
//    //    public static String ip = "192.168.0.101";
//    public static String ip = "172.16.1.100";
//    public static int port = 8888;
//    String fileStr="i:\\a_100mb.mp4";

    /**
     * 可以上传大文件
     * 101MB传得很快
     * <p>
     * 待实现:
     * <p>
     * 1统计上传时间ok
     * 101MB 两个java程序本地上传 花费2s
     * <p>
     * 2进度ok
     * 百分比=当前上传大小追加后/总大小*100+%
     * <p>
     * 3上传文件名ok
     * 客户端和服务端都要获取到文件名
     * socket无法知道传输文件名，服务端只能通过上一接口传过来文件名
     * <p>
     * 4重命名ok
     * 服务端以当前时间命名
     * <p>
     * 5通过结束标记结束上传
     * 服务端不发字符而是发结束标记来表示上传完成
     * <p>
     * 6多线程并发上传
     * 先开多线程
     * 再试线程池
     * <p>
     * 7代码优化catch中处理异常或失败情况，finally中关资源
     *
     * @param args
     */
    public static void main(String[] args) {
        try {

            Socket socket = new Socket(UploadPicServer.ip, UploadPicServer.port);
            if (socket == null) {
                System.out.println("超时");
                return;
            }
            long start = System.currentTimeMillis();

            File file = new File(UploadPicServer.fileStr);
            String absolutePath = file.getAbsolutePath();
            String filename = absolutePath.substring(absolutePath.lastIndexOf(File.separator) + 1, absolutePath.length());
            System.out.println("文件路径==>" + absolutePath);//absolutePath==>i:\a_100mb.mp4
            System.out.println("客户端文件名==>" + filename);//absolutePath==>i:\a_100mb.mp4
            float length = file.length();
            System.out.println("length==>" + length);

            FileInputStream fis = new FileInputStream(UploadPicServer.fileStr);

            OutputStream socketOut = socket.getOutputStream();

            int len;
            float lenCount = 0;//用于计算上传进度
            byte[] fisBuf = new byte[1024];
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0); // 设置精确到小数点后2位
            while ((len = fis.read(fisBuf)) != -1) {
/*                lenCount += len;
                float bfbD = lenCount / length * 100;
                String result = numberFormat.format(bfbD);
                System.out.println("上传进度：" + result + "%");//*/

                socketOut.write(fisBuf, 0, len);
            }
            socket.shutdownOutput();//结束标记

            InputStream socketIs = socket.getInputStream();
//            byte[] socketIsBuf = new byte[128];
            int socketIsLen;
            if ((socketIsLen = socketIs.read()) == -1) {//上传完成
                long end = System.currentTimeMillis();
                long time = end - start;
                String timeStr = time / 1000 + "s";
                System.out.println("上传完成");
                System.out.println("上传用时" + timeStr);
            } else {//没上传完
                System.out.println("没上传完");
            }
            fis.close();
            socket.close();
        } catch (Exception e) {
            System.out.println("超时");
        }
    }
}
